﻿using HandyControl.Controls;
using MiniExcelLibs;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using SqlSugar;
using HandyControl.Data;
using System.Windows;
using Newtonsoft.Json.Linq;
using HandyControl.Tools.Extension;

namespace BaseUi.ViewModel
{
    public class RoleViewModel : BindableBase
    {
        private List<RoleBase> dataList = new List<RoleBase>();
        /// <summary>
        /// 列表数据
        /// </summary>
        public List<RoleBase> DataList { get => dataList; set { SetProperty(ref dataList, value); } }

        #region 筛选
        private string info1;
        public string Info1 { get => info1; set { SetProperty(ref info1, value); } }
        #endregion

        public ICommand UpdateListCommand { get => new DelegateCommand(UpdateList); }
        /// <summary>
        /// 更新信息
        /// </summary>
        public void UpdateList()
        {
            try
            {
                var dbData = AuthDb.db.Queryable<RoleBase>()
                .WhereIF(!string.IsNullOrWhiteSpace(Info1), o => o.Name.StartsWith(Info1.Trim()))
                .OrderBy(o => o.Id)
                .ToList();

                DataList = dbData;
            }
            catch (Exception ex)
            {
                Growl.Error("加载数据失败：" + ex.Message);
            }
            finally
            {

            }
        }

        public ICommand SeeCommand { get => new DelegateCommand<RoleBase>(See); }
        public void See(RoleBase obj)
        {
            RoleEditView.Show(obj, CrudEnum.Read);
        }

        public ICommand AddCommand { get => new DelegateCommand(Add); }
        public void Add()
        {
            var isUp = RoleEditView.Show(new RoleBase(), CrudEnum.Create);
            if (isUp)
            {
                UpdateList();
                Growl.Success("创建成功");
            }
        }

        public ICommand UpdateCommand { get => new DelegateCommand<RoleBase>(Update); }
        public void Update(RoleBase obj)
        {
            var isUp = RoleEditView.Show(obj, CrudEnum.Update);
            if (isUp)
            {
                UpdateList();
                Growl.Success("更新成功");
            }
        }


        public ICommand DelCommand { get => new DelegateCommand<RoleBase>(Del); }
        public void Del(RoleBase obj)
        {
            Growl.Ask($"是否删除角色[{obj.Name}]!", isConfirmed =>
            {
                if (isConfirmed)
                {
                    try
                    {
                        var isContains = AuthDb.db.Queryable<UserBase>().Select(o => o.RoleIds).ToList().SelectMany(o => o).Contains(obj.Id);
                        if (isContains)
                        {
                            Growl.Info($"此角色被用户使用中，无法删除");
                            return true;
                        }

                        AuthDb.db.Deleteable(obj).ExecuteCommand();
                    }
                    catch (Exception ex)
                    {
                        Growl.Error($"删除失败：{ex.ToString()}");
                        return true;
                    }

                    UpdateList();
                    Growl.Success("删除成功");
                }
                return true;
            });
        }
    }
}
